///////// Mason D. Hill
/// CPE 301, ASSIGNMENT #4
#include <avr/io.h>

   #define F_CPU 8000000UL  // 8 MHz
   #include <util/delay.h>



int main(void)
{
    uint8_t i = 0;

   
   
    DDRB = 0b11111111;///Sets the direction Used for LED bits 0-7
    DDRD = 0b11111111;///Enable output of all.

    // TCCR0B = 0b00000110;// Reads an external clock on the falling edge. NOTE: This is from PORTD7 connected to PORTD4.
   
   


    TCCR0A = 0b10100011; // WGM = 111 com = non inverting
    TCCR0B = 0b00001001; // No prescaling
   
        OCR0B = 0; // duty cycle
        OCR0A = 255; // period




/// OCR1B/OCR1A = duty cycle/ period
    OCR1B = 0; // duty cycle
    OCR1A = 255; // period


    TCCR1A = 0b10100011; // WGM = 1111 com = non inverting
    TCCR1B = 0b00011001; // No prescaling


    OCR2B = 0; // duty cycle
    OCR2A = 255; // period


    TCCR2A = 0b10100011; // WGM = 111 com = non inverting
    TCCR2B = 0b00001001; // No prescaling




    int counterseq = 1; // Holds which step to go through


   
    while(1)
    {




if(counterseq==1){

    OCR0A--; // Decrease the period
    if(OCR0A==25){ //Do not want period to drop below duty,
    OCR0A = 255;    // So I must set it up again
    OCR0B++; // Increase the duty cycle
    if(OCR0B>230){  OCR0B = 0; counterseq++; } // Once it achieves 90% duty, turn LED off, and go to next counter
    }        

    }        
    if(counterseq==2){
    OCR1A--; // Decrease the period
    if(OCR1A==25){ //Do not want period to drop below duty,
    OCR1A = 255;// So I must set it up again
    OCR1B++;  // Increase the duty cycle
    if(OCR1B>230){  OCR1B = 0; counterseq++; } // Once it achieves 90% duty, turn LED off, and go to next counter
       
    }
       
    }
   
                   
    if(counterseq==3){    
    OCR2A--; // Decrease the period
    if(OCR2A==25){ //Do not want period to drop below duty,
    OCR2A = 255;// So I must set it up again
    OCR2B++;  // Increase the duty cycle
    if(OCR2B>230){  OCR2B = 0; counterseq++; OCR0B=255;} // Once it achieves 90% duty, turn LED off, and go to next counter
       
    }        
    }                
               

               
               
    if(counterseq==4){ // Merges the colors of timer 0 and timer 1
    // 0&1
    OCR0B--; // Decrease the magnitude of color 0
    OCR1B++;// Increase the magnitude of color 1
    if(OCR0B==0) { counterseq++; }
                   
               
    }
               
    if(counterseq==5){ // Merges the colors of timer 1 and timer 2
    // 1&2
    OCR1B--; // Decrease the magnitude of color 1
    OCR2B++; // Increase the magnitude of color 2
    if(OCR1B==0) { counterseq++; }
                               
                               
    }
                           
                           
    if(counterseq==6){ // Merges the colors of timer 2 and timer 0
    // 2&0
    OCR2B--; // Decrease the magnitude of color 2
    OCR0B++; // Increase the magnitude of color 0
    if(OCR2B==0) { counterseq = 4; } // Loop through the color spectrum indefinitely.
                                               
                                               
    }
   
                           
                           
                           
               
               
               
               
               
               
               
               
if(counterseq<=3) _delay_us(240); // Due to the slow nature of the first three sequences, we need to not have big delay
               
       
        if(counterseq>3)   _delay_ms(10); // Fewer steps, so need bigger delay
               
               


    }

   

    return 0;
}